import App from './App.vue'
import router from './router/index'
import ElementUI from 'element-plus';
import moment from 'moment'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import 'element-plus/dist/index.css'
import './index.css'
import {
    createApp
} from 'vue';
import axios from './utils/http';
// vue-cookies配置
import cookies from 'vue-cookies'

import {
    ElMessage,
    ElMessageBox
} from 'element-plus'

const app = createApp(App)
app.use(router)
// element-plus汉化
app.use(ElementUI, {
    locale: zhCn
})
//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
    let cookie = window.document.cookie;
    if (!cookie && (to.path !== '/login')) {
        next('/login');
    } else {
        // 验证登录信息
        axios.post('/org/userResource/userMsg').then(result => {
            if (!result.isOk && result.code === "401") {
                next('/login');
                return;
            } else if (!result.isOk) {
                ElMessage({
                    message: result.msg,
                    type: 'error',
                })
                return
            }
            const userMsg = result.data;
            localStorage.setItem("userName", userMsg.user.fullname)
            localStorage.setItem("userID", userMsg.user.userId)
            localStorage.setItem("loginFrom", userMsg.user.from)
            localStorage.setItem("account", userMsg.user.account)
            localStorage.setItem("authorities", JSON.stringify(userMsg.user.authorities))
        })
        next();
    }
});
// element plus js组件引入
app.config.globalProperties.$ElMessage = ElMessage;
app.config.globalProperties.$ElMessageBox = ElMessageBox;

app.config.globalProperties.$axios = axios;
app.config.globalProperties.$moment = moment;
app.config.globalProperties.$cookies = cookies

app.mount('#app')